package com.ruksis.motogeek;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;

public class MotoService extends Service {

	public static final int REGISTER_CLIENT = 1;
	public static final int UNREGISTER_CLIENT = 2;
	
	public static final int START_DATA_COLLECTION = 10;
	public static final int STOP_DATA_COLLECTION = 11;

	public static final int ACCEL_DATA_BUNDLE = 32;
	public static final int SPEED_DATA_BUNDLE = 33;
	public static final int LEAN_DATA_BUNDLE = 34;
		
	private Messenger _messenger = new Messenger(new IncomingHandler());
	private SensorManager _sensorManager = (SensorManager) getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
	private LocationManager _locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
	private Messenger _client;
	private SensorEventListener _listener;
	private GPSListener _gpsListener;
	
	@Override
	public IBinder onBind(Intent intent) {
		return _messenger.getBinder();
	}
	
	class IncomingHandler extends Handler {
		@Override
        public void handleMessage(Message msg) {

			if ( msg.what == REGISTER_CLIENT) {
				_client = msg.replyTo;
			}
			
			if ( msg.what == START_DATA_COLLECTION ) {
				_listener = new AccelerometerListener();
				_sensorManager.registerListener(_listener, _sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), SensorManager.SENSOR_DELAY_NORMAL);
				_sensorManager.registerListener(_listener, _sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
				_sensorManager.registerListener(_listener, _sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
				_gpsListener = new GPSListener();
				_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100,1,_gpsListener);
				MotoDataProcessor.startQueue(new Messenger[] { _client });
			}
		
			if ( msg.what == STOP_DATA_COLLECTION ) {
				_sensorManager.unregisterListener(_listener);
				_locationManager.removeUpdates(_gpsListener);
				MotoDataProcessor.stopQueue();
			}
			
		}
	}
	
	class AccelerometerListener implements SensorEventListener {

		public void onSensorChanged(SensorEvent event) {
			MotoDataProcessor.push(event);
		}

		public void onAccuracyChanged(Sensor sensor, int accuracy) {
		}
		
	}
	
	class GPSListener implements LocationListener {

		public void onLocationChanged(Location location) {
			//location.
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

		public void onProviderEnabled(String provider) {
		}

		public void onProviderDisabled(String provider) {
		}

		
	}

}
